package com.sun.service.impl;

import com.sun.dao.CommentDao;
import com.sun.pojo.Comment;
import com.sun.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: glorysun
 * @date: 2020/8/10
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentDao commentDao;

    @Override
    public List<Comment> selectCommentByBlogId(Long blogId) {
        List<Comment> topComment=commentDao.selectCommentByBlogIdTop(blogId);   //一级评论
        List<Comment> secondComment=new ArrayList<>();  //二级评论

        for(int i=0;i<topComment.size();i++){
            List<Comment> allSecondComment=new ArrayList<>();   //所有次级评论
            secondComment=commentDao.selectCommentByBlogIdSecond(blogId, topComment.get(i).getId());    //一级评论下的二级评论
            ss(secondComment,blogId,allSecondComment);
            topComment.get(i).setReplyComments(allSecondComment);  //一级评论中存放次级评论
        }

        return topComment;
    }

    public List<Comment> ss(List<Comment> secondComment,Long blogId,List<Comment> allSecondComment){
        List<Comment> lowComment=new ArrayList<>();

        if(secondComment.size()==0)
            return null;
        for(int i=0;i<secondComment.size();i++){    //遍历二级评论
            allSecondComment.add(secondComment.get(i));     //将二级评论存放再总评论中
            lowComment=commentDao.selectCommentByBlogIdSecond(blogId, secondComment.get(i).getId());    //根据二级评论查询下一级评论
            if(lowComment==null){
                continue;
            }
        }
        return ss(lowComment,blogId,allSecondComment);
    }

    @Override
    public int insertComment(Comment comment) {
        comment.setCreateTime(new Date());
        return commentDao.insertComment(comment);
    }
}
